from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from celery import Celery

client = AcsClient('LTAITVuCDBVd96RG', 'hos39zZUMquazeCnG3RHIRIg6JHFXY', 'cn-hangzhou')
app = Celery(broker='amqp://guest@localhost//')  # 消息队列的url


@app.task
def send(phone, code):
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('dysmsapi.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https')  # https | http
    request.set_version('2017-05-25')
    request.set_action_name('SendSms')

    request.add_query_param('RegionId', "cn-hangzhou")
    request.add_query_param('PhoneNumbers', "%s" % phone)
    request.add_query_param('SignName', "南神博客")
    request.add_query_param('TemplateCode', "SMS_167880069")
    request.add_query_param('TemplateParam', "{\"sedo\":\"%d\"}" % code)

    response = client.do_action_with_exception(request)
    # python2:  print(response)
    print(str(response, encoding='utf-8'))


if __name__ == '__main__':
    send(18775541428, 852000)
